package org.gjy.m8.stream;

import com.google.common.collect.Lists;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 如筛选、切片、映射、查找、匹配和归约
 *
 * @author 宫静雨
 * @version 1.0
 * @since 2023-08-03 16:33:20
 */
public class Stream1Test {
    private static final Logger log = LoggerFactory.getLogger(Stream1Test.class);
    private static final List<Domain> list = Domain.list();

    @Test
    @SuppressWarnings("all")
    public void test7() {
        Lists.newArrayList(1, 2, 3, 4, 5)
                .stream()
                .collect(Collectors.toList())
                .forEach(System.out::println);
    }

    @Test
    public void test6() {
        Integer collect = Lists.newArrayList(1, 2, 3, 4, 5)
                .stream()
                .collect(new MyCollectorImpl());
        System.out.println(collect);
    }

    @Test
    public void test5() {
        list.stream().collect(Collectors.toMap(Domain::getId, v -> v))
                .forEach((k, v) -> log.info("{}:{}", k, v));
    }

    @Test
    public void test4() {
        ArrayList<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5);
        Integer reduce = list.stream().reduce(0, Integer::sum);
        System.out.println("reduce = " + reduce);
    }

    @Test
    public void test3() {
        ArrayList<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5);
        boolean b = list.stream().anyMatch(i -> {
            System.out.println("i = " + i);
            return i >= 3;
        });
        System.out.println("eee");
        System.out.println(b);
    }

    @Test
    public void test2() {
        System.out.println(Lists.newArrayList("Hello", "World")
                .stream()
                .map(s -> s.split(""))
                .flatMap(Arrays::stream)
                .collect(Collectors.toList()));
    }

    @Test
    public void test1() {
        List<String> collect = Lists.newArrayList("Hello", "World")
                .stream()
                .flatMap(s -> Stream.of(s.split("")))
                .collect(Collectors.toList());
        System.out.println(collect);
    }
}
